import { ecs } from "../../../../core/ecs/ECS";

/**
 * 生命值组件
 * 
 * 通用组件：可用于 Player、Enemy、NPC、建筑等任何有生命值的实体
 * 
 * @example
 * const player = ecs.getEntity(Player);
 * player.HealthComponent.takeDamage(30);
 * if (!player.HealthComponent.isAlive()) {
 *     console.log('Dead!');
 * }
 */
@ecs.register('HealthComponent')
export class HealthComponent extends ecs.Comp {
    /** 当前生命值 */
    public current: number = 100;
    
    /** 最大生命值 */
    public max: number = 100;
    
    /**
     * 重置组件数据
     * 对象池回收时调用
     */
    reset(): void {
        this.current = 100;
        this.max = 100;
    }
    
    /**
     * 设置生命值
     */
    set(current: number, max: number): this {
        this.current = current;
        this.max = max;
        return this;
    }
    
    /**
     * 受到伤害
     */
    takeDamage(amount: number): void {
        if (amount < 0) {
            console.warn('Damage amount cannot be negative');
            return;
        }
        
        this.current = Math.max(0, this.current - amount);
    }
    
    /**
     * 治疗
     */
    heal(amount: number): void {
        if (amount < 0) {
            console.warn('Heal amount cannot be negative');
            return;
        }
        
        this.current = Math.min(this.max, this.current + amount);
    }
    
    /**
     * 检查是否存活
     */
    isAlive(): boolean {
        return this.current > 0;
    }
    
    /**
     * 获取生命值百分比 (0-100)
     */
    getPercentage(): number {
        return this.max > 0 ? (this.current / this.max) * 100 : 0;
    }
    
    /**
     * 是否满血
     */
    isFullHealth(): boolean {
        return this.current >= this.max;
    }
    
    /**
     * 增加最大生命值
     */
    increaseMaxHealth(amount: number): void {
        this.max += amount;
        this.current += amount;  // 增加最大值时，当前值也增加
    }
}

